package com.group.admin.service.impl;

import com.group.common.utils.JwtUtil;
import com.group.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.common.utils.PageUtils;
import com.group.common.utils.Query;

import com.group.admin.dao.ShopUserDao;
import com.group.admin.entity.ShopUserEntity;
import com.group.admin.service.ShopUserService;

import javax.servlet.http.HttpSession;


@Service("shopUserService")
public class ShopUserServiceImpl extends ServiceImpl<ShopUserDao, ShopUserEntity> implements ShopUserService {
    @Autowired
    RedisTemplate redisTemplate;


    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    ShopUserDao shopUserDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShopUserEntity> page = this.page(
                new Query<ShopUserEntity>().getPage(params),
                new QueryWrapper<ShopUserEntity>()
        );

        return new PageUtils(page);
    }



    @Override
    public R login(String phone, String msg, HttpSession session) {
        Object o = redisTemplate.opsForValue().get(phone);
        //验证码正确登录成功
        if (o.toString().equals(msg)){
            ShopUserEntity phone1 = this.getOne(new QueryWrapper<ShopUserEntity>().eq("user_phone", phone));
            if (phone1==null){
                return R.error("该手机号未注册");
            }
            Integer userId = phone1.getUserId();
            session.setAttribute("loginUser",phone1);
            String token = jwtUtil.createJWT(msg,phone,"admin");
            return R.ok("登录成功！").put("token",token).put("userid",userId);
        }
        return R.error("手机号或验证码错误。");
    }



}